package avicit.bdp.dms.perm.rest;

import avicit.bdp.common.service.dto.DatasourcePermDTO;
import avicit.bdp.dms.perm.service.DatasourcePermService;
import avicit.platform6.api.sysuser.dto.SysRole;
import avicit.platform6.core.rest.msg.QueryReqBean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2022-11-03
 * @类说明：DatasourcePermRest
 * @修改记录：
 * @注意事项：
 * @主要功能：数据源权限管理Rest接口
 */
@RestController
@Api(tags = "数据源权限管理Rest接口")
@RequestMapping("/api/bdp/dms/perm/datasource")
public class DatasourcePermRest {
    @Autowired
    private DatasourcePermService dsPermService;

    @ApiOperation("按条件分页查询角色信息")
    @PostMapping("/searchByPage/v1")
    public ResponseMsg<QueryRespBean<SysRole>> searchSysRoleByPage(@RequestBody QueryReqBean<SysRole> queryReqBean){
        ResponseMsg responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(dsPermService.searchSysRoleByPage(queryReqBean));
        return responseMsg;
    }

    /**
     * 新增数据源权限
     * @param permList 支持批量授权
     * @return
     */
    @ApiOperation(value = "新增数据源权限")
    @PostMapping("/addDatasourcePerm")
    public ResponseMsg addDatasourcePerm(@RequestBody List<DatasourcePermDTO> permList) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.dsPermService.addDatasourcePerm(permList);

        return responseMsg;
    }

    /**
     * 移除数据源权限
     * @param permList
     * @return
     */
    @PostMapping("/removeDatasourcePerm")
    @ApiOperation(value = "移除数据源权限")
    public ResponseMsg removeDatasourcePerm(@RequestBody List<DatasourcePermDTO> permList) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        this.dsPermService.removeDatasourcePerm(permList);

        return responseMsg;
    }

    /**
     * 根据数据源ID删除权限(业务模块删除数据源时同步删除权限)
     * @param datasourceId
     * @return
     */
    @ApiOperation(value = "根据节点ID删除权限")
    @ApiImplicitParam(value = "数据源id", name = "datasourceId", dataType = "String", required = true)
    @GetMapping("/deleteDatasourcePermById")
    public ResponseMsg<Integer> deletePermById(@RequestParam String datasourceId) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        this.dsPermService.deletePermById(datasourceId);

        return responseMsg;
    }

    /**
     * 根据角色查询权限列表(提供给业务模块使用)
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色查询权限列表")
    @ApiImplicitParam(value = "角色id", name = "roleId", dataType = "String", required = true)
    @GetMapping("/getGrantedPermList")
    public ResponseMsg<List<DatasourcePermDTO>> getGrantedPermList(@RequestParam String roleId) {
        ResponseMsg<List<DatasourcePermDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.dsPermService.getGrantedPermList(roleId));

        return responseMsg;
    }
}
